package cn.mall.auth.controller;

import cn.mall.auth.annotation.MyPermission;
import cn.mall.auth.dto.RoleMenuDTO;
import cn.mall.auth.dto.RolePermissionDTO;
import cn.mall.auth.service.IRoleService;
import cn.mall.auth.domain.Role;
import cn.mall.auth.query.RoleQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role" )
@MyPermission(name = "角色管理")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @MyPermission(name = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody Role role) {
        try {
            if ( role.getId() != null)
                roleService.update(role);
            else
                roleService.insert(role);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    @MyPermission(name = "根据id删除")
    public AjaxResult remove(@PathVariable("id" ) Long id) {
        try {
            roleService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
    */
    @PatchMapping
    @MyPermission(name = "批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            roleService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
    * 根据Id获取用户
    * @param id
    * @return
    */
    @GetMapping("/{id}" )
    @MyPermission(name = "根据id查询")
    public AjaxResult loadById(@PathVariable("id" ) Long id) {
        try {
            Role role =roleService.loadById(id);
            return AjaxResult.me().setResultObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    @MyPermission(name = "查询所有")
    public AjaxResult loadAll() {

        try {
            List< Role> list = roleService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @MyPermission(name = "角色分页查询", desc = "角色分页查询")
    public AjaxResult pageList(@RequestBody RoleQuery query) {
        try {
            PageList<Role> pageList = roleService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }


    /**
     * 根据角色id查询标识的集合，前端做回显
     * @param roleId
     * @return
     */
    @GetMapping("/permission/{roleId}")
//    @MyPermission(name = "根据角色id查询标识的集合", desc = "根据角色id查询标识的集合，前端做回显")
    public AjaxResult loadPermissionSnsByRoleId(@PathVariable("roleId")Long roleId){
        try {
            List<String> permissionSns = roleService.loadPermissionSnsByRoleId(roleId);
            return AjaxResult.me().setResultObj(permissionSns);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("返回标识失败!"+e.getMessage());
        }

    }


    /**
     * 更新权限
     * @param dto
     * @return
     */
    @PostMapping("/permission")
    @MyPermission(name = "更新权限", desc = "更新权限")
    public AjaxResult saveRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.saveRolePermission(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }


    /**
     * 更新菜单
     * @param dto
     * @return
     */
    @PostMapping("/menu")
    @MyPermission(name = "更新菜单", desc = "更新菜单")
    public AjaxResult saveRoleMenu(@RequestBody RoleMenuDTO dto){
        try {
            roleService.saveRoleMenu(dto);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置菜单失败!"+e.getMessage());
        }
    }

    @GetMapping("/menu/{roleId}")
    public AjaxResult loadMenuIdByRoleId(@PathVariable("roleId")Long roleId){
        try {
            List<Long> MenuId=roleService.loadMenuIdByRoleId(roleId);
            return AjaxResult.me().setResultObj(MenuId);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("返回菜单失败!"+e.getMessage());
        }
    }

    @GetMapping("/ids")
    public AjaxResult loadIds(){
        try {
            List<Long> roleIds = roleService.loadRoleIds();
            return AjaxResult.me().setResultObj(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("返回菜单失败!"+e.getMessage());
        }
    }

}
